const Token = require('../utils/token');
const UserController = require('../controllers/users')
const router = require('koa-router')()
router.prefix('/users')

const ctr = new UserController()

router.get('/publicLogin', async (ctx) => {
  try {
    const { username, password } = ctx.request.query;
    const result = await ctr.login(username, password);
    ctx.status = 200;
    console.log('console_2024年07月13日 23时22分04秒：', result)
    ctx.body = result;
  } catch (err) {
    console.error('Database query failed:', err.message);
    ctx.status = 500;
    ctx.body = { error: 'Database query failed' };
  }
});

router.get('/publicGetUsers', async function (ctx, next) {
  try {
    const { offset, limit } = ctx.request.query;
    const result = await ctr.getUsers(offset,limit);
    ctx.status = 200;
    ctx.body = result;
  } catch (err) {
    console.error('Database query failed:', err.message);
    ctx.status = 500;
    ctx.body = { error: 'Database query failed' };
  }
})

router.get('/publicAdd',async function (ctx,next) {
  try {
    for(let i=0;i<100;i++){
      const user={
        username:'user'+i,
        password:'1111'+i,
        email:'111111333'+i,
        updateAt:new Date(),
        createAt:new Date()
      }
      await ctr.addUser(user)
    }
    ctx.status = 200;
    ctx.body = 'success';
  } catch (error) {
    console.error('Database query failed:', err.message);
    ctx.status = 500;
    ctx.body = { error: 'Database query failed' };
  }
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})

module.exports = router
